using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class ls : Feathertail.Command
    {
        private FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;

            if (!_Client.FeathertailState.ContainsKey(Helpers.CurrentDirectoryKey))
            {
                _Client.FeathertailState[Helpers.CurrentDirectoryKey] = "/";
            }
        }

        public string HandleInput(string[] CmdLine)
        {
            StringBuilder output = new StringBuilder();

            #region Check for commandline options
            bool displayType = false;
            bool displayItemID = false;
            bool displayAssetID = false;

            string dir2List = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];

            for(int i=1; i<CmdLine.Length; i++)
            {
                string option = CmdLine[i];
                switch (option.ToLower())
                {
                    case "/itemid":
                        displayItemID = true;
                        break;
                    case "/assetid":
                        displayAssetID = true;
                        break;
                    case "/type":
                        displayType = true;
                        break;
                    default:
                        dir2List = option;
                        break;
                }
            }
            #endregion

            // Download inventory information
            InventoryFolder iFolder = _Client.Inventory.getFolder(dir2List);
            if (iFolder == null)
            {
                return "Could not find : " + dir2List;
            }

            if (!iFolder.RequestDownloadContents(false, true, true).RequestComplete.WaitOne(15000, false))
            {
                output.AppendLine("Timeout while downloading directory information...");
                return output.ToString();
            }

            output.AppendLine("Contents of: " + dir2List);
            output.AppendLine("--------------------------------------------------");
            if (!dir2List.Equals("/"))
            {
                output.AppendLine("..");
            }


            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryFolder)
                {
                    InventoryFolder folder = (InventoryFolder)ib;

                    output.Append("[DIR] ");

                    if (displayItemID)
                    {
                        output.Append("<itemid:" + folder.FolderID.ToStringHyphenated() + "> ");
                    }
                    if (displayType)
                    {
                        output.Append("<type:" + folder.GetDisplayType() + "> ");
                    }

                    output.AppendLine(folder.Name);
                }
                else
                {
                    InventoryItem item = (InventoryItem)ib;

                    if (displayItemID)
                    {
                        output.Append("<itemid:" + item.ItemID.ToStringHyphenated() + "> ");
                    }
                    if (displayAssetID)
                    {
                        output.Append("<assetid:" + item.AssetID.ToStringHyphenated() + "> ");
                    }
                    if (displayType)
                    {
                        output.Append("<type:" + item.GetDisplayType() + "> ");
                    }

                    output.AppendLine(item.Name);
                }
            }

            return output.ToString();
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "ls", "dir" };
        }

        public string GetSummaryText()
        {
            return "List directory contents";
        }

        public string GetHelpText()
        {
            return "List contents of the current directory. Options: /itemid /assetid /type";
        }

        #endregion
    }
}
